package com.tjbank.cssys.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CreditInstallment;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CreditInstallmentService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

@Api(tags = "CkLevelController", description = "信用卡个性化分期")
@RestController
@RequestMapping("/installment")
public class CreditInstallmentController {

    @Autowired
    private CkLogService ckLogService;
    @Autowired
    private CreditInstallmentService creditInstallmentService;

    private static final Logger logger = LoggerFactory.getLogger(CreditDerateController.class);

    @ApiOperation("查看分期详情")
    @GetMapping("/selectDetails")
    public CommonResult selectDetails(@RequestParam(value = "id" ,required = false) Long id, HttpServletRequest request) {
        CreditInstallment creditInstallment = creditInstallmentService.selectByPrimaryKey(id);

        return CommonResult.success(creditInstallment, "123");
    }


    @ApiOperation("获取信用卡个性化分期数据查询管理")
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1") Integer page  , @RequestParam(defaultValue ="5") Integer pageSize , CreditInstallment creditInstallment, HttpServletRequest request) {
        PageInfo<CreditInstallment> creditInstallments = creditInstallmentService.selectList(creditInstallment,page,pageSize);
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        if (creditInstallments!=null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditInstallment));
            ckLog.setOperationType("获取信用卡个性化分期数据查询管理");
            ckLog.setResult("success:200");
            logger.info("获取信用卡个性化分期数据查询管理结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(creditInstallments);
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }



    @ApiOperation("录入信用卡个性化分期数据")
    @PostMapping("/add")
    public CommonResult addInsert(@RequestBody CreditInstallment creditInstallment, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("录入信用卡个性化分期数据");

        int insert = creditInstallmentService.insert(creditInstallment);
        if (insert != 0) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditInstallment));
            ckLog.setOperationType("录入信用卡个性化分期数据");
            ckLog.setResult("success:200");
            logger.info("录入信用卡个性化分期数据结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(insert, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }

    @ApiOperation("逻辑删除信用卡减免数据")
    @PostMapping("/delete")
    public CommonResult delete(CreditInstallment creditInstallment, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("逻辑删除信用卡个性化分期");

        int i = creditInstallmentService.UpdatecardStatue(creditInstallment);
        if (String.valueOf(i)!=null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditInstallment));
            ckLog.setOperationType("逻辑删除信用卡个性化分期数据");
            ckLog.setResult("success:200");
            logger.info("逻辑删除信用卡个性化分期数据结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(i, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }

    @ApiOperation("修改信用卡个性化分期审批状态")
    @PostMapping("/audit")
    public CommonResult UpdateAudit(CreditInstallment creditInstallment, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("审批信用卡个性化分期开始");
        int i = creditInstallmentService.UpdateAudit(creditInstallment);
        if (i != 0) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditInstallment));
            ckLog.setOperationType("审批信用卡个性化分期成功");
            ckLog.setResult("success:200");
            logger.info("审批信用卡个性化分期结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(i, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }







}
